FROM ubuntu:20.04

# build参数
ARG filebrowser_user=ht
ARG filebrowser_password=ht

# 元数据
LABEL maintainer="jiejie" 
LABEL email="jiejietop@gmail.com"
LABEL org.label-schema.schema-version="1.2.0"
LABEL org.label-schema.build-date="2023-03"
LABEL org.label-schema.name="jiejie:ubuntu20.04"
LABEL org.label-schema.description="jiejie:ubuntu20.04"
LABEL org.label-schema.docker.cmd="\
    docker run -it --rm --privileged \
    --net=host \
    --ipc=host \
    --pid=host \
    -e DISPLAY=$DISPLAY \
    -v /tmp/.X11-unix:/tmp/.X11-unix \
    -d jiejie:ubuntu20.04 \
    "

# 打印平台信息

USER root

ARG homedir=/root
ARG workspace=/root/workspace

# tuna.tsinghua.edu.cn
# aliyun.com
RUN  sed -i s@/ports.ubuntu.com/@/mirrors.tuna.tsinghua.edu.cn/@g /etc/apt/sources.list \
    && sed -i s@/security.ubuntu.com/@/mirrors.tuna.tsinghua.edu.cn/@g /etc/apt/sources.list

RUN apt-get update

RUN apt-get install -y \
    locales \
    tar \
    wget \
    zsh \
    git \
    curl \
    vim \
    tmux

RUN locale-gen zh_CN.UTF-8
ENV \
    LANG=zh_CN.UTF-8 \
    DEBIAN_FRONTEND=noninteractive \
    TERM=xterm

# 拉取 filebrowser 二进制文件
RUN cd /tmp && \
    arch=$(dpkg --print-architecture) && \
    # wget https://github.com/filebrowser/filebrowser/releases/download/v2.23.0/linux-${arch}-filebrowser.tar.gz \
    wget https://gitee.com/jiejietop/filebrowser-release/raw/master/linux-${arch}-filebrowser.tar.gz \
    && tar -zxvf linux-${arch}-filebrowser.tar.gz \
    && mv filebrowser /usr/bin

# 配置 filebrowser
RUN /usr/bin/filebrowser -d /etc/filebrowser.db config init && \
    /usr/bin/filebrowser -d /etc/filebrowser.db config set --address 0.0.0.0 && \
    /usr/bin/filebrowser -d /etc/filebrowser.db config set --locale zh-cn && \
    /usr/bin/filebrowser -d /etc/filebrowser.db config set --log /var/log/filebrowser.log && \
    /usr/bin/filebrowser -d /etc/filebrowser.db config set --port 8888 && \
    /usr/bin/filebrowser -d /etc/filebrowser.db config set --root ${workspace} && \
    /usr/bin/filebrowser -d /etc/filebrowser.db users add ${filebrowser_user} ${filebrowser_password} --perm.admin

# ########################## zsh环境配置 ################################
RUN chsh -s /bin/zsh

RUN git clone https://gitee.com/jiejieTop/ohmyzsh.git ${homedir}/.oh-my-zsh
RUN git clone https://gitee.com/jiejieTop/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-${homedir}/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
RUN git clone https://gitee.com/jiejieTop/zsh-autosuggestions.git ${ZSH_CUSTOM:-${homedir}/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
RUN git clone https://gitee.com/jiejieTop/git-open.git ${ZSH_CUSTOM:-${homedir}/.oh-my-zsh/custom}/plugins/git-open
RUN cp ${homedir}/.oh-my-zsh/templates/zshrc.zsh-template ${homedir}/.zshrc
RUN sed -i 's/plugins=(git)/plugins=(z git sudo extract vi-mode git-open safe-paste command-not-found colored-man-pages zsh-syntax-highlighting vscode zsh-autosuggestions)/g' ${homedir}/.zshrc \
    && sed -i 's/# DISABLE_AUTO_UPDATE="true"/DISABLE_AUTO_UPDATE="true"/g' ${homedir}/.zshrc \
    && sed -i 's/# DISABLE_AUTO_TITLE="true"/DISABLE_AUTO_TITLE="true"/g' ${homedir}/.zshrc

# ########################## tmux环境配置 ################################
RUN echo 'set-option -g mouse on' > ${homedir}/.tmux.conf
RUN echo 'set -g pane-border-status top' >> ${homedir}/.tmux.conf
RUN echo 'set-option -g allow-rename off' >> ${homedir}/.tmux.conf
RUN echo 'set -g pane-border-format "#{pane_index} #T #{pane_current_command}"' >> ${homedir}/.tmux.conf
RUN echo 'bind-key -T prefix R send-keys "renamePane" C-m' >> ${homedir}/.tmux.conf
RUN echo '' >> ${homedir}/.tmux.conf


# ########################## 配置 filebrowser 自启动 ################################
RUN echo '#!/bin/bash' > /etc/init.d/filebrowser && \
    echo '/usr/bin/filebrowser -d /etc/filebrowser.db' >> /etc/init.d/filebrowser && \
    chmod +x /etc/init.d/filebrowser && \
    update-rc.d filebrowser defaults

# ########################## 工作目录 ################################
WORKDIR ${homedir}

CMD [ "/bin/bash", "-c", "/etc/init.d/filebrowser", "/bin/bash" ]